<?php

/**
 * Enter description here...
 *
 */
class keyword_class extends seo_base_class 
{
	private $_word_fq = 2;
	private $_content = '';

	public function __construct()
	{
		parent::__construct();
	}

	public function set_content($content)
	{
		$this->_content = $content;
	}

	public function get_key_word()
	{
		$key_word_arr = array();
		if(empty($this->_content))
		{
			return $key_word_arr;
		}
		$short_word_arr = $this->get_short_word_arr($this->_content);
		$this->high_fq_short_word($short_word_arr);
		if(empty($short_word_arr))
		{
			return $key_word_arr;
		}
		
		$short_words = "";
		foreach($short_word_arr as $short_word=>$fq)
		{
			$short_words .= '"'.addslashes($short_word).'",';
		}
		$short_words = trim($short_words, ',');
		$keyword_arr = array();
		foreach($this->get_key_word_by_short_word($short_words) as $val)
		{
			if(strpos($this->_content, $val['keyword']) !== false)
			{
				$keyword_arr[] = $val['keyword'];
			}
		}
		return $keyword_arr;
	}

	private function get_key_word_by_short_word($short_word)
	{
		$sql = "SELECT keyword_index, keyword FROM {$this->_db_name}.www_keyword_tbl WHERE keyword_index IN({$short_word}) ORDER BY NULL";
		return db_query_sql($sql, $this->_server_id);
	}
	
	private function high_fq_short_word(&$short_word_arr)
	{
		foreach($short_word_arr as $word=>$fq)
		{
			if($fq < $this->_word_fq)
			{
				unset($short_word_arr[$word]);
			}
		}
		return true;
	}
	
	private function get_short_word_arr($content)
	{
		$content = preg_replace('/[\s"\']/', '', $content);
		$l = strlen($content);
		$m = 0;
		$short_word_arr = array();
		while($m < $l)
		{
			$t = $m + (ord($content{$m}) > 128 ? 2 : 1);
			$_m = $t;
			$t += @ord($content{$t}) > 128 ? 2 : 1;
			@$short_word_arr[substr($content, $m, $t - $m)] ++;
			$m = $_m;

		}
		return $short_word_arr;
	}
	
	public function add_keyword($keyword)
	{
		global $G_SQC;
		if(empty($keyword))
		{
			return false;
		}
		$word_arr = ($this->get_simplified_pingying($keyword));
		foreach($word_arr[0] as $key=>$val)
		{
			if(empty($word_arr[1][$key]))
			{
				$word_arr[1][$key] = $word_arr[2][$key] = $val;
			}
		}
		list($keyword_abc, $keyword_py) = array(implode($word_arr[1]), implode($word_arr[2]));
		$keyword_index = array_keys($this->get_short_word_arr(cut_gbk_str($keyword, 4)));
		$sql = "INSERT IGNORE {$this->_db_name}.www_keyword_tbl (keyword_index, keyword_abc_index, keyword_py_index, keyword, add_time)VALUES('{$keyword_index[0]}', '{$keyword_abc}', '{$keyword_py}', '{!{$keyword}!}', {$G_SQC->time()})";
		db_query_sql($sql, $this->_server_id);
		return db_get_insert_id();
	}
	
	public function get_simplified_pingying($simplified_str)
	{
		return parent::get_simplified_pingying($simplified_str);
	}
}

